package com.ch.clinking.adapter.Order;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.ch.clinking.R;
import com.ch.clinking.Utils.NetUtils.OkHttpUtil;
import com.ch.clinking.bean.OrderInfo;

import java.util.List;

public class EditOrderAdapter extends RecyclerView.Adapter<EditOrderAdapter.MyViewHolder> {

    public List<OrderInfo> orderInfoList;
    private LayoutInflater layoutInflater;
    private Context context;
    private Integer operation = 0;
    public Integer batch = 0;
    public Integer changeOperateNum = 1;

    public EditOrderAdapter(Context context, List<OrderInfo> orderInfoList) {
        this.orderInfoList = orderInfoList;
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
    }


    @NonNull
    @Override

    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.list_edit_order_sku_layout, parent, false);
        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public int getItemCount() {
        return orderInfoList.size();
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, @SuppressLint("RecyclerView") int position) {

        OrderInfo orderInfo = orderInfoList.get(position);
        Glide.with(context).load(OkHttpUtil.getInstance().getIMG_URL() + orderInfo.getImage()).into(holder.image);
        holder.title.setText(orderInfo.getTitle());
        // 设置初始值时移除监听器，避免触发不必要的回调
        holder.addNum.setTag(null);
        if (orderInfo.getFinalNum() != 0) {
            holder.addNum.setText(Integer.toString(orderInfo.getFinalNum()));
        } else {
            holder.addNum.setText("");
        }
        holder.proposedNum.setText(Integer.toString(orderInfo.getProposedNum()));

        TextWatcher watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {}

            @Override
            public void afterTextChanged(Editable editable) {
                String c = editable.toString();
                if (c.equals("")) {
                    c = "0";
                }
                int change = Integer.parseInt(c);
                orderInfo.setFinalNum(change);
            }
        };

        holder.addNum.addTextChangedListener(watcher);
        holder.addNum.setTag(watcher);

    }

    @SuppressLint("NotifyDataSetChanged")
    public void flash() {
        notifyDataSetChanged();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        ImageView image;
        TextView title, proposedNum;
        EditText addNum;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);

            this.image = itemView.findViewById(R.id.edit_order_sku_img);
            this.title = itemView.findViewById(R.id.edit_order_sku_title);
            this.addNum = itemView.findViewById(R.id.edit_order_sku_final_num);
            this.proposedNum = itemView.findViewById(R.id.edit_order_proposed_num);

        }
    }

}
